package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.model.TbSpecification;
import com.dongdongshop.model.TbSpecificationOption;
import com.dongdongshop.model.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.vo.SpecificationOptionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecificationOptionServiceImpl implements SpecificationOptionService {
    @Resource
    private TbSpecificationOptionMapper specificationOptionMapper;
    @Override
    public List<SpecificationOptionVO> seleteBySpecId(Long id) {
        TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = optionExample.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> specificationOptionList = specificationOptionMapper.selectByExample(optionExample);
        if(specificationOptionList == null){
            return null;
        }
        List<SpecificationOptionVO> collect = specificationOptionList.stream().map(op -> {
            SpecificationOptionVO specificationOptionVO = new SpecificationOptionVO();
            BeanUtils.copyProperties(op, specificationOptionVO);
            return specificationOptionVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpecificationOptionVO> getAllOption() {
        List<TbSpecificationOption> specificationOptionList = specificationOptionMapper.selectByExample(null);
        if(specificationOptionList == null){
            return null;
        }
        List<SpecificationOptionVO> collect = specificationOptionList.stream().map(op -> {
            SpecificationOptionVO specificationOptionVO = new SpecificationOptionVO();
            BeanUtils.copyProperties(op, specificationOptionVO);
            return specificationOptionVO;
        }).collect(Collectors.toList());
        return collect;
    }
}
